<?php

namespace youmbe\phpword\assets;

use PhpOffice\PhpWord\Shared\Converter;
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
use Yii;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\i18n\Formatter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\ListItem as ListItemStyle;
use PhpOffice\PhpWord\SimpleType\TextAlignment;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\TablePosition;
use youmbe\phpword\widgets\DetailView;
use PhpOffice\PhpWord\Shared\Html as PhpWordHtml;
use PhpOffice\PhpWord\Style\Font as FontStyle;

/**
 * Asset bundle for the Word widget files.
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class WordAsset extends AssetBundle {

    public $titleStyles = [];
    public $fontStyles = [];
    public $headerStyles = [];
    public $footerStyles = [];

    /**
     * 文档默认样式
     * @param PhpWord $view
     */
    public function setDefaultStyles($view) {
        //默认样式
        $view->setDefaultFontName('仿宋');
        $view->setDefaultFontSize(14);
        $view->setDefaultParagraphStyle([
            'indentation' => [
                'firstLine' => Converter::pointToTwip(28),
                'left' => 0,
            ],
            'spacing' => Converter::pointToTwip(24),
            'spacingLineRule' => LineSpacingRule::EXACT,
        ]);
    }

    /**
     * 格式化样式数组
     * @param array $styles
     */
    public function formatStyles($styles) {
        $fontStyle = ArrayHelper::getValue($styles, 'fontStyle');
        $paragraphStyle = ArrayHelper::getValue($styles, 'paragraphStyle');
        $borderStyle = ArrayHelper::getValue($styles, 'borderStyle');
        if (!empty($borderStyle) && is_array($borderStyle)) {
            if (is_array($paragraphStyle)) {
                $paragraphStyle = ArrayHelper::merge($paragraphStyle, $borderStyle);
            } else {
                $paragraphStyle = $borderStyle;
            }
        }
        return [
            'fontStyle' => $fontStyle,
            'paragraphStyle' => $paragraphStyle
        ];
    }

    /**
     * Setting properties for word file
     */
    public function registerStyle() {
        $view = $this->view;
        //默认样式
        $this->setDefaultStyles($view);

        //标题样式
        $titleStyles = $this->getTitleStyles();
        foreach ($titleStyles as $level => $style) {
            $_style = $this->formatStyles($style);
            $view->addTitleStyle($level, $_style['fontStyle'], $_style['paragraphStyle']);
        }

        //自定义页眉页脚
        $headerFooterStyles = $this->getHeaderFooterStyles();
        foreach ($headerFooterStyles as $name => $style) {
            $_style = $this->formatStyles($style);
            $view->addFontStyle($name, $_style['fontStyle'], $_style['paragraphStyle']);
        }

        //标记
        $view->addFontStyle('underline', [
            'underline' => FontStyle::UNDERLINE_SINGLE
        ]);
        //标记
        $view->addFontStyle('表格正文', [
            'size' => 12,
        ], [
            'indentation' => [
                'firstLine' => 0,
            ],
            'indent' => 0,
            'lineHeight' => 1,
        ]);
        //标记
        $view->addParagraphStyle('center', [
            'alignment' => 'center',
        ]);
    }

    /**
     * 页眉页脚样式
     * @return array
     */
    public function getHeaderFooterStyles() {
        return [
            'Header' => [
                'fontStyle' => [
                    'name' => 'Calibri',
                    'size' => 5.25,
                    'color' => '333333',
                    'allCaps' => true,
                ],
                'paragraphStyle' => [
                    'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::RIGHT,
                    'indentation' => [
                        'firstLine' => 0,
                        'left' => 0,
                    ],
                    'lineHeight' => 1,
                    'spacingLineRule' => LineSpacingRule::AUTO,
                    'suppressAutoHyphens' => true,
                ],
                'borderStyle' => [
                    'borderBottomSize' => 10,
                    'borderBottomStyle' => \youmbe\phpword\Style\Border::BORDER_STYLE_THIN_THICK_SMALL_GAP,
                    'borderColor' => '666666'
                ],
            ],
            'Footer' => [
                'fontStyle' => [
                    'name' => 'Calibri',
                    'size' => 9,
                ],
                'paragraphStyle' => [
                    'indentation' => [
                        'firstLine' => 0,
                        'left' => 0,
                    ],
                    'lineHeight' => 1,
                    'spacingLineRule' => LineSpacingRule::AUTO,
                    'suppressAutoHyphens' => true,
                    'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER
                ]
            ]
        ];
    }

    /**
     * 标题样式
     * @return type
     */
    public function getTitleStyles() {
        return [
            1 => [
                'fontStyle' => [
                    'name' => '华文中宋',
                    'size' => 28,
                    'bold' => true
                ],
                'paragraphStyle' => [
                    'indentation' => [
                        'firstLine' => 0,
                    ],
                    'spacing' => 1,
                    'spaceBefore' => Converter::pointToTwip(28),
                    'spaceAfter' => Converter::pointToTwip(28),
                    'alignment' => Jc::CENTER,
                    'textAlignment' => TextAlignment::CENTER,
                ]
            ],
            2 => [
                'fontStyle' => [
                    'name' => '华文中宋',
                    'size' => 16,
                    'bold' => true
                ],
                'paragraphStyle' => [
                    'indentation' => [
                        'firstLine' => 0,
                    ],
                    'spacing' => 1,
                    'spaceBefore' => Converter::pointToTwip(16),
                    'spaceAfter' => Converter::pointToTwip(16),
                    'alignment' => Jc::CENTER,
                    'textAlignment' => TextAlignment::CENTER,
                ]
            ],
        ];
    }

    /**
     * 多级列表样式
     * @return type
     */
    public static function getListStyles() {
        return [
            [
                'font' => [
                    'name' => '黑体',
                    'size' => 14,
                ],
                'list' => [
                    'pStyle' => 'Heading3',
                    'format' => 'decimal', //bullet|decimal|upperRoman|lowerRoman|upperLetter|lowerLetter
                    'text' => '%1.', // Numbering level text e.g. %1 for nonbullet or bullet character.
                    'left' => 0, //缩进
                    'hanging' => 0, //悬挂缩进
//                    'hint' => 'eastAsia', //编号文本类型 Font content type, default, eastAsia, or cs.
                    'tabPos' => 0, //制表符位置
                    'suffix' => 'nothing', //Content between numbering symbol and paragraph text tab|space|nothing.
                    'alignment ' => \PhpOffice\PhpWord\SimpleType\Jc::BOTH,
                    'font' => '黑体',
//                    'restart' => true, //重新编号
//                    'start' => '', //编号
                ],
                'paragraph' => [
                    'indentation' => [
                        'firstLine' => Converter::pointToTwip(28),
                    ],
                    'spacing' => Converter::pointToTwip(24),
                    'spacingLineRule' => LineSpacingRule::EXACT,
                    'spaceBefore' => Converter::pointToTwip(14),
                    'spaceAfter' => Converter::pointToTwip(14),
                ]
            ],
            [
                'font' => null,
                'list' => [
                    'pStyle' => 'Heading4',
                    'format' => 'decimal', //bullet|decimal|upperRoman|lowerRoman|upperLetter|lowerLetter
                    'text' => '%1.%2.', // Numbering level text e.g. %1 for nonbullet or bullet character.
                    'left' => 0, //缩进
                    'hanging' => 0, //悬挂缩进
                    'tabPos' => 0, //制表符位置
                    'suffix' => 'nothing', //Content between numbering symbol and paragraph text tab|space|nothing.
                ],
                'paragraph' => [
                    'indentation' => [
                        'firstLine' => Converter::pointToTwip(28),
                    ],
                    'spacing' => Converter::pointToTwip(24),
                    'spacingLineRule' => LineSpacingRule::EXACT,
                ]
            ],
            [
                'font' => null,
                'list' => [
                    'pStyle' => 'Heading5',
                    'format' => 'decimal', //bullet|decimal|upperRoman|lowerRoman|upperLetter|lowerLetter
                    'text' => '%1.%2.%3.', // Numbering level text e.g. %1 for nonbullet or bullet character.
                    'left' => 0, //缩进
                    'hanging' => 0, //悬挂缩进
                    'tabPos' => 0, //制表符位置
                    'suffix' => 'nothing', //Content between numbering symbol and paragraph text tab|space|nothing.
                ],
                'paragraph' => [
                    'indentation' => [
                        'firstLine' => Converter::pointToTwip(28),
                    ],
                    'spacing' => Converter::pointToTwip(24),
                    'spacingLineRule' => LineSpacingRule::EXACT,
                ]
            ]
        ];
    }

    public static function getMultiLevelStyle($level = null, $name = null) {
        $listStyles = static::getListStyles();
        if ($level === null) {
            return ArrayHelper::getColumn($listStyles, 'list');
        }
        $styles = ArrayHelper::getValue($listStyles, $level);
        if ($name) {
            return ArrayHelper::getValue($styles, $name);
        }
        return $styles;
    }

// * @method Style\Paragraph addParagraphStyle(string $styleName, mixed $styles)
// * @method Style\Font addFontStyle(string $styleName, mixed $fontStyle, mixed $paragraphStyle = null)
// * @method Style\Font addLinkStyle(string $styleName, mixed $styles)
// * @method Style\Font addTitleStyle(mixed $depth, mixed $fontStyle, mixed $paragraphStyle = null)
// * @method Style\Table addTableStyle(string $styleName, mixed $styleTable, mixed $styleFirstRow = null)
// * @method Style\Numbering addNumberingStyle(string $styleName, mixed $styles)

    public function registerParagraphStyle($styleName) {
        $view->addParagraphStyle($styleName, $styles);
    }

    public function registerFontStyle($styleName) {
        $view->addFontStyle($styleName, $fontStyle, $paragraphStyle);
    }

    public function registerLinkStyle($styleName) {
        $view->addLinkStyle($styleName, $styles);
    }

    public function setTitleStyles($view) {
        $view->addTitleStyle($styleName, $fontStyle, $paragraphStyle);
    }

    public function registerTableStyle($styleName) {
        $view->addTableStyle($styleName, $styleTable, $styleFirstRow);
    }

    public function registerNumberingStyle($styleName) {
        $view->addNumberingStyle($styleName, $styles);
    }

}
